package cn.jcet.module.bus.service.impl;

import cn.jcet.module.bus.domain.Goods;
import cn.jcet.module.bus.domain.Inport;
import cn.jcet.module.bus.mapper.GoodsMapper;
import cn.jcet.module.bus.mapper.InportMapper;
import cn.jcet.module.bus.service.IInportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鲜磊
 * @since 2019-12-25
 */
@Service
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements IInportService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public boolean save(Inport entity) {
        //根据商品编号查询
        Goods goods = goodsMapper.selectById(entity.getGoodsid());
        goods.setNumber(entity.getNumber()+goods.getNumber());
        goodsMapper.updateById(goods);
        //保持进货信息
        return super.save(entity);
    }

    @Override
    public boolean updateById(Inport entity) {
        //根据编号查询
        Goods goods = goodsMapper.selectById(entity.getGoodsid());
        goods.setNumber(entity.getGoodsid()+goods.getNumber());
        goodsMapper.updateById(goods);
        //更新进货单
        return super.updateById(entity);
    }


    @Override
    public boolean removeById(Serializable id) {
        //生成进货id
        Inport inport = this.baseMapper.selectById(id);
        //根据商品id查询
        Goods goods = goodsMapper.selectById(inport.getGoodsid());
        //当前库存-进货单数量
        inport.setNumber(inport.getNumber() - goods.getNumber());
        this.baseMapper.updateById(inport);
        return super.removeById(id);
    }
}
